package com.ruoyi.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysClass;
import com.ruoyi.system.domain.SysTeacher;
import com.ruoyi.system.service.ISysTeacherService;

/**
 * 教师管理 控制层
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/teacher")
public class SysTeacherController extends BaseController
{
    @Autowired
    private ISysTeacherService teacherService;

    /**
     * 获取教师列表
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTeacher teacher)
    {
        startPage();
        List<SysTeacher> list = teacherService.list(teacher);
        return getDataTable(list);
    }

    /**
     * 根据教师编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:query')")
    @GetMapping(value = "/{teacherId}")
    public AjaxResult getInfo(@PathVariable Long teacherId)
    {
        return success(teacherService.selectById(teacherId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:add')")
    @Log(title = "教师管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysTeacher teacher)
    {

        teacher.setCreateBy(getUsername());
        int rows = teacherService.insert(teacher);
        return toAjax(rows);
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:edit')")
    @Log(title = "教师管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysTeacher teacher)
    {
        teacher.setUpdateBy(getUsername());
        return toAjax(teacherService.update(teacher));
    }

    /**
     * 删除教师
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:remove')")
    @Log(title = "教师管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{teacherId}")
    public AjaxResult remove(@PathVariable Long teacherId)
    {
        return toAjax(teacherService.deleteById(teacherId));
    }

    /**
     * 获取班级列表，供调取
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:list')")
    @GetMapping("/listClassUnBind")
    public TableDataInfo listClassUnBind(Long teacherId)
    {
        startPage();
        List<SysClass> list = teacherService.listClassUnBind(teacherId);
        return getDataTable(list);
    }

    /**
     * 绑定班级
     */
    @PreAuthorize("@ss.hasPermi('system:teacher:bind')")
    @Log(title = "教师管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{teacherId}/bind-class/{classIds}")
    public AjaxResult bindClass(@PathVariable Long teacherId, @PathVariable Long[] classIds)
    {
        teacherService.insert(teacherId, classIds);
        return success();
    }
}